﻿using System.Collections.Generic;
using System.Web.Mvc;
using LightWeb.Infrastructure.Repositories;
using LightWeb.MvcWeb.Areas.SmartPfa.Models;
using System.Data;

namespace LightWeb.MvcWeb.Areas.SmartPfa.Controllers
{
    public class StockController : Controller
    {
        //
        // GET: /SmartPfa/Stock/

        public ActionResult Index()
        {
            var list = new List<Models.StockCashFlowModel>();
            using (OurTransactionScope ts = new OurTransactionScope())
            {
                var context = DataContext.Current;
                foreach (var item in context.Read<DomainModel.StockCashFlow>(null, null))
                {
                    list.Add(StockCashFlowModel.FromDomainModel(item));
                }
                ts.Complete();
            }
            return View(list);
        }

        //
        // GET: /SmartPfa/Stock/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /SmartPfa/Stock/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SmartPfa/Stock/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SmartPfa/Stock/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /SmartPfa/Stock/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SmartPfa/Stock/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /SmartPfa/Stock/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [HttpGet]
        public ActionResult PreViewData(string id)
        {
            using (var excel = new AocsoftToolkit.ExcelUtility(System.AppDomain.CurrentDomain.BaseDirectory + @"\Upload\Stock Cash Floow.xlsx"))
            {
                Dictionary<string, DataTable> data = new Dictionary<string, DataTable>();
                foreach (var item in excel.GetSheetNames())
                {
                    data.Add(item, excel.GetDataBySheet(item));
                }
                return View(data);
            }
        }

        //[HttpGet]
        //public ActionResult PreViewData(string id)
        //{
        //    using (var excel = new AocsoftToolkit.ExcelUtility(System.AppDomain.CurrentDomain.BaseDirectory + @"\Upload\Stock Cash Floow.xlsx"))
        //    {
        //        return View(excel.GetDataBySheet(id));
        //    }
        //}
    }
}
